package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        Appointment appointment1 = new Appointment();
        appointment1.setCustomerName(appointment.getCustomerName());
        appointment1.setCustomerPhone(appointment.getCustomerPhone());
        appointment1.setAppointmentTime(appointment.getAppointmentTime());
        appointment1.setCreateTime(new Date());
        appointment1.setLicensePlate(appointment.getLicensePlate());
        appointment1.setCarSeries(appointment.getCarSeries());
        appointment1.setServiceType(appointment.getServiceType());
        appointment1.setInfo(appointment.getInfo());
        appointmentMapper.insert(appointment1);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment appointment2 = this.get(appointment.getId());
        //预约中才能修改
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment2.getStatus())){
            throw new BusinessException("预约中才能修改");
        }
        appointment2.setCustomerName(appointment.getCustomerName());
        appointment2.setCustomerPhone(appointment.getCustomerPhone());
        appointment2.setAppointmentTime(appointment.getAppointmentTime());
        appointment2.setLicensePlate(appointment.getLicensePlate());
        appointment2.setCarSeries(appointment.getCarSeries());
        appointment2.setServiceType(appointment.getServiceType());
        appointment2.setInfo(appointment.getInfo());
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public void arrivalOp(Long id) {
        Appointment appointment = this.get(id);
        //预约中才能到店
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("预约中才能到店");
        }
        appointmentMapper.arrivalOp(id,Appointment.STATUS_ARRIVAL,new Date());
    }

    @Override
    public void cancelOp(Long id) {
        Appointment appointment = this.get(id);
        //预约中才能取消
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("预约中才能取消");
        }
        appointmentMapper.cancelOp(id,Appointment.STATUS_CANCEL);

    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void updateByStatement(Appointment appointment) {
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void cancelOp(Long appointmentId, Integer status) {
        appointmentMapper.cancelOp(appointmentId,status);
    }
}

